/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.kj;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.just.dusin.ooii.component.Source;
import org.dusin.webcoal.exception.OOIIException;
import org.just.dusin.ooii.util.DeviceSimulator;

/**
 * 每隔一定时间自动发送消息
 * @author Administrator
 */
public class AutoMineDataSource extends Source {
    
    private static Logger logger = Logger.getLogger(AutoMineDataSource.class.getName());
    DeviceSimulator ds;  //5分钟数据发生器;
   
    private String cityName;
    private String countyName;
    private String mineName;
    private double hourlyPlan;
    
    /**
     * 重复运行次数
     */
        
    private int repeat;
    public int getRepeat() {
        return repeat;
    }
    public void setRepeat(int repeat) {
        this.repeat = repeat;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCountyName() {
        return countyName;
    }

    public void setCountyName(String countyName) {
        this.countyName = countyName;
    }

    public DeviceSimulator getDs() {
        return ds;
    }

    public void setDs(DeviceSimulator ds) {
        this.ds = ds;
    }

    public double getHourlyPlan() {
        return hourlyPlan;
    }

    public void setHourlyPlan(double hourlyPlan) {
        this.hourlyPlan = hourlyPlan;
    }

    public String getMineName() {
        return mineName;
    }

    public void setMineName(String mineName) {
        this.mineName = mineName;
    }
   
    
    

   
    /**
     * 消息时间间隔，单位秒
     */
    private int interval = 10;

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }
   

    @Override
    public void initComponent(Properties props) {
    }

    @Override
    public void destroyComponent() throws Exception {
    }

    @Override
    public void run() {
        int i = 0;
        Date now = null;
        
        while (i<repeat) {
            i++;
            double currentOut = ds.getCurrentOutputOfTotal();
            double ratio = currentOut/hourlyPlan;
            ratio=(ratio+0.005)*100;
            byte rate = (byte)ratio;
            String msg = DataPackage.packCLSC(i,cityName,countyName,mineName,ds.getTimeString(),"t",(double)currentOut,rate);
            try {
                sink.process(msg);
                logger.info(msg);
                Thread.sleep(interval*300);
            } catch (OOIIException ex) {
                logger.error(ex.getMessage());
            }catch(InterruptedException ie){
                
            }
        }
    }
}
